//
//  VipFilterView.m
//  fitnessmanage
//
//  Created by wujian on 2017/12/6.
//  Copyright © 2017年 zongquankeji. All rights reserved.
//

#import "VipFilterView.h"
#import "MOFSPickerManager.h"

@interface VipFilterView()

@property(nonatomic,strong)NSMutableArray *ageArray;

@end

@implementation VipFilterView

-(id)initWithFrame:(CGRect)frame{
    
    self  = [super initWithFrame:frame];
    
    if (self) {
        
        self.filtDict = [[NSMutableDictionary alloc] init];
        
        UIView *alphaView = [[UIView alloc] init];
        alphaView.backgroundColor = [CSBlackColor colorWithAlphaComponent:0.4];
        [self addSubview:alphaView];
        self.alphaView = alphaView;
        
        /**
         白色
         */
        UIView *whiteView = [[UIView alloc] init];
        whiteView.backgroundColor = CSWhiteColor;
        [self addSubview:whiteView];
        self.whiteView = whiteView;
        
        /**
         线条
         */
        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = LineBackGroundColor;
        [whiteView addSubview:lineView];
        self.lineView = lineView;
        /**
         重置按钮
         */
        UIButton *cancleBtn = [[UIButton alloc] init];
        [cancleBtn addTarget:self action:@selector(reset)];
        [cancleBtn setTitle:@"重置"];
        cancleBtn.titleLabel.font = MidBigFont;
        [cancleBtn setTitleColor:CSColorFromRGB(0x0a0a0a)];
        [whiteView addSubview:cancleBtn];
        self.cancleBtn = cancleBtn;
        
        /**
         确认按钮
         */
        UIButton *sureBtn = [[UIButton alloc] init];
        [sureBtn addTarget:self action:@selector(sureBtnClick)];
        [sureBtn setTitle:@"确认"];
        sureBtn.titleLabel.font = MidBigFont;
        [sureBtn setTitleColor:CSColorFromRGB(0x0a0a0a)];
        sureBtn.backgroundColor = ButtonBackGroundColor;
        [whiteView addSubview:sureBtn];
        self.sureBtn = sureBtn;
        
        /**
         今日按钮
         */
        UIButton *todayBtn = [[UIButton alloc] init];
        todayBtn.tag = 1;
        [todayBtn addTarget:self action:@selector(shortTimeBtnClick:)];
        todayBtn.layer.cornerRadius = 2;
        todayBtn.layer.borderColor = CSColorFromRGB(0xdbdbdb).CGColor;
        todayBtn.layer.borderWidth = 1.0;
        todayBtn.layer.masksToBounds = YES;
        todayBtn.titleLabel.font = MidFont;
        todayBtn.contentEdgeInsets = UIEdgeInsetsMake(10, 15, 10, 15);
        [todayBtn setTitle:@"今日"];
        [todayBtn setTitleColor:CSColorFromRGB(0x333333)];
        [whiteView addSubview:todayBtn];
        self.todayBtn = todayBtn;
        
        
        /**
         最近7天
         */
        UIButton *weekBtn = [[UIButton alloc] init];
        weekBtn.tag = 2;
        [weekBtn addTarget:self action:@selector(shortTimeBtnClick:)];
        weekBtn.layer.cornerRadius = 2;
        weekBtn.layer.borderColor = CSColorFromRGB(0xdbdbdb).CGColor;
        weekBtn.layer.borderWidth = 1.0;
        weekBtn.layer.masksToBounds = YES;
        weekBtn.titleLabel.font = MidFont;
        weekBtn.contentEdgeInsets = UIEdgeInsetsMake(10, 15, 10, 15);
        [weekBtn setTitle:@"最近7天"];
        [weekBtn setTitleColor:CSColorFromRGB(0x333333)];
        [whiteView addSubview:weekBtn];
        self.weekBtn = weekBtn;
        
        
        /**
         最近30天
         */
        UIButton *monBtn = [[UIButton alloc] init];
        [monBtn addTarget:self action:@selector(shortTimeBtnClick:)];
        monBtn.tag = 3;
        monBtn.layer.cornerRadius = 2;
        monBtn.layer.borderColor = CSColorFromRGB(0xdbdbdb).CGColor;
        monBtn.layer.borderWidth = 1.0;
        monBtn.layer.masksToBounds = YES;
        monBtn.titleLabel.font = MidFont;
        monBtn.contentEdgeInsets = UIEdgeInsetsMake(10, 15, 10, 15);
        [monBtn setTitle:@"最近30天"];
        [monBtn setTitleColor:CSColorFromRGB(0x333333)];
        [whiteView addSubview:monBtn];
        self.monBtn = monBtn;

        /**
         开始日期
         */
        UIButton *startBtn = [[UIButton alloc] init];
        startBtn.layer.cornerRadius = 2;
        startBtn.layer.masksToBounds = YES;
        startBtn.backgroundColor = BackGroundColor;
        startBtn.titleLabel.font = MidFont;
        startBtn.contentEdgeInsets = UIEdgeInsetsMake(10, 20, 10, 20);
        startBtn.tag = 1;
        [startBtn addTarget:self action:@selector(timeSelect:)];
        [startBtn setTitle:@"开始日期"];
        [startBtn setTitleColor:CSColorFromRGB(0x999999)];
        [whiteView addSubview:startBtn];
        self.startBtn = startBtn;
        
        /**
         结束日期
         */
        UIButton *endBtn = [[UIButton alloc] init];
        endBtn.layer.cornerRadius = 2;
        endBtn.layer.masksToBounds = YES;
        endBtn.backgroundColor = BackGroundColor;
        endBtn.titleLabel.font = MidFont;
        endBtn.contentEdgeInsets = UIEdgeInsetsMake(10, 15, 10, 15);
        endBtn.tag = 2;
        [endBtn addTarget:self action:@selector(timeSelect:)];
        [endBtn setTitle:@"结束日期"];
        [endBtn setTitleColor:CSColorFromRGB(0x999999)];
        [whiteView addSubview:endBtn];
        self.endBtn = endBtn;;
        
        
        UIView *timeLineView = [[UIView alloc] init];
        timeLineView.backgroundColor = LineBackGroundColor;
        [whiteView addSubview:timeLineView];
        self.timeLineView = timeLineView;
        
        /**
         注册日期
         */
        UILabel *timeLabel = [[UILabel alloc] init];
        timeLabel.font = MidFont;
        timeLabel.text = @"注册日期";
        timeLabel.textColor = CSColorFromRGB(0x666666);
        [whiteView addSubview:timeLabel];
        self.timeLabel = timeLabel;
        
        /**
         注册日期
         */
        UIImageView *timeLeftLineView = [[UIImageView alloc] init];
        timeLeftLineView.image = [UIImage imageNamed:@"line-blackleft"];
        [whiteView addSubview:timeLeftLineView];
        self.timeLeftLineView = timeLeftLineView;
        
        /**
         注册日期
         */
        UIImageView *timeRightLineView = [[UIImageView alloc] init];
        timeRightLineView.image = [UIImage imageNamed:@"line-blackright"];
        [whiteView addSubview:timeRightLineView];
        self.timeRightLineView = timeRightLineView;
        
        /**
         年龄
         */
        UILabel *ageLabel = [[UILabel alloc] init];
        ageLabel.font = MidFont;
        ageLabel.text = @"年龄";
        ageLabel.textColor = CSColorFromRGB(0x666666);
        [whiteView addSubview:ageLabel];
        self.ageLabel = ageLabel;
        
        /**
         年龄
         */
        UIImageView *ageLeftLineView = [[UIImageView alloc] init];
        ageLeftLineView.image = [UIImage imageNamed:@"line-blackleft"];
        [whiteView addSubview:ageLeftLineView];
        self.ageLeftLineView = ageLeftLineView;;
        
        /**
         年龄
         */
        UIImageView *ageRightLineView = [[UIImageView alloc] init];
        ageRightLineView.image = [UIImage imageNamed:@"line-blackright"];
        [whiteView addSubview:ageRightLineView];
        self.ageRightLineView = ageRightLineView;
        
        /**
         开始年龄
         */
        UIButton *startAge = [[UIButton alloc] init];
        startAge.tag = 1;
        [startAge addTarget:self action:@selector(ageSelect:)];
        startAge.layer.cornerRadius = 2;
        startAge.layer.masksToBounds = YES;
        startAge.backgroundColor = BackGroundColor;
        startAge.titleLabel.font = MidFont;
        startAge.contentEdgeInsets = UIEdgeInsetsMake(10, 20, 10, 20);
        [startAge setTitle:@"0"];
        [startAge setTitleColor:CSColorFromRGB(0x999999)];
        [whiteView addSubview:startAge];
        self.startAge = startAge;
        
        UIView *ageLineView = [[UIView alloc] init];
        ageLineView.backgroundColor = LineBackGroundColor;
        [whiteView addSubview:ageLineView];
        self.ageLineView = ageLineView;
        
        /**
         结束年龄
         */
        UIButton *endAge = [[UIButton alloc] init];
        endAge.layer.cornerRadius = 2;
        endAge.tag = 2;
        [endAge addTarget:self action:@selector(ageSelect:)];
        endAge.layer.masksToBounds = YES;
        endAge.backgroundColor = BackGroundColor;
        endAge.titleLabel.font = MidFont;
        endAge.contentEdgeInsets = UIEdgeInsetsMake(10, 20, 10, 20);
        [endAge setTitle:@"30"];
        [endAge setTitleColor:CSColorFromRGB(0x999999)];
        [whiteView addSubview:endAge];
        self.endAge = endAge;
        
        /**
         性别
         */
        UILabel *sexLabel = [[UILabel alloc] init];
        sexLabel.font = MidFont;
        sexLabel.text = @"性别";
        sexLabel.textColor = CSColorFromRGB(0x666666);
        [whiteView addSubview:sexLabel];
        self.sexLabel = sexLabel;
        
        /**
         性别
         */
        UIImageView *sexLeftLineView = [[UIImageView alloc] init];
        sexLeftLineView.image = [UIImage imageNamed:@"line-blackleft"];
        [whiteView addSubview:sexLeftLineView];
        self.sexLeftLineView = sexLeftLineView;
        
        /**
         性别
         */
        UIImageView *sexRightLineView = [[UIImageView alloc] init];
        sexRightLineView.image = [UIImage imageNamed:@"line-blackright"];
        [whiteView addSubview:sexRightLineView];
        self.sexRightLineView = sexRightLineView;;
        
        /**
         性别
         */
        UIButton *manBtn = [[UIButton alloc] init];
        manBtn.tag = 1;
        [manBtn setTitle:@"男"];
        [manBtn setTitleColor:CSColorFromRGB(0x0a0a0a)];
        manBtn.titleLabel.font = SmallFont;
        [manBtn addTarget:self action:@selector(sexBtnClick:)];
        [manBtn setImage:@"img_man"];
        [manBtn setSelectedImage:@"img_man_choose"];
        [whiteView addSubview:manBtn];
        self.manBtn = manBtn;
        
        /**
         性别
         */
        UIButton *womanBtn = [[UIButton alloc] init];
        [womanBtn setTitle:@"女"];
        womanBtn.tag = 2;
        [womanBtn addTarget:self action:@selector(sexBtnClick:)];
        [womanBtn setTitleColor:CSColorFromRGB(0x0a0a0a)];
        womanBtn.titleLabel.font = SmallFont;
        [womanBtn setImage:@"img_girl"];
        [womanBtn setSelectedImage:@"img_girl_choose"];
        [whiteView addSubview:womanBtn];
        self.womanBtn = womanBtn;
        
        /**
         标题
         */
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.font = [UIFont systemFontOfSize:17.0];
        titleLabel.textColor = CSColorFromRGB(0x212121);
        titleLabel.text = @"会员信息";
        [whiteView addSubview:titleLabel];
        self.titleLabel = titleLabel;
        
        
        /**
         关闭按钮
         */
        UIButton *closeBtn = [[UIButton alloc] init];
        [closeBtn addTarget:self action:@selector(closeBtnClick)];
        [closeBtn setImage:@"icon_botclose"];
        [whiteView addSubview:closeBtn];
        self.closeBtn = closeBtn;
        
        
        [self.filtDict setObject:@"0-30" forKey:@"age"];
        
        
        [self setConstraints];
    }
    
    return self;
}

-(void)setConstraints{
    
    [self.alphaView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(self);
    }];
    
    
    /**
     白色背景
     */
    [self.whiteView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.width.bottom.mas_equalTo(self);
    }];
    
    /**
     线条
     */
    [self.lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
    
        make.left.width.mas_equalTo(self.whiteView);
        
        make.bottom.mas_equalTo(self.cancleBtn.mas_top);
        
        make.height.mas_equalTo(1);
    }];
    
    /**
     重置按钮
     */
    [self.cancleBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.left.bottom.mas_equalTo(self.whiteView);
        
        make.height.mas_equalTo(50);
    }];
    
    /**
     确认按钮
     */
    [self.sureBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.lineView);
        
        make.width.mas_equalTo(self.cancleBtn);
        
        make.left.mas_equalTo(self.cancleBtn.mas_right);
        
        make.bottom.right.mas_equalTo(self.whiteView);
    }];
    
    /**
     今日按钮
     */
    [self.todayBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.weekBtn);
        
        make.right.mas_equalTo(self.weekBtn.mas_left).offset(-20);
        
        make.width.mas_equalTo(self.monBtn);
    }];
    
    
    /**
     最近7天
     */
    [self.weekBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(self.whiteView);
        
        make.bottom.mas_equalTo(self.cancleBtn.mas_top).offset(-30);
        
        make.width.mas_equalTo(self.monBtn);
    }];
    
    
    /**
     最近30天
     */
    [self.monBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.weekBtn);
        
        make.left.mas_equalTo(self.weekBtn.mas_right).offset(20);
    }];
    
    
    [self.timeLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.mas_equalTo(self.whiteView);
        
        make.centerY.mas_equalTo(self.startBtn);
        
        make.height.mas_equalTo(1);
        
        make.width.mas_equalTo(10);
    }];
    
    /**
     开始日期
     */
    [self.startBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.todayBtn);
        
        make.bottom.mas_equalTo(self.todayBtn.mas_top).offset(-25);
        
        make.right.mas_equalTo(self.timeLineView.mas_left).offset(-3);
    }];
    
    /**
     结束日期
     */
    [self.endBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.startBtn);
        
        make.left.mas_equalTo(self.timeLineView.mas_right).offset(3);
        
        make.height.mas_equalTo(self.startBtn);
        
        make.right.mas_equalTo(self.monBtn);
    }];
    
    [self.timeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.mas_equalTo(self.whiteView);
        
        make.bottom.mas_equalTo(self.startBtn.mas_top).offset(-20);
    }];
    
    [self.timeLeftLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.mas_equalTo(self.timeLabel);
        
        make.right.mas_equalTo(self.timeLabel.mas_left).offset(-10);
    }];
    
    [self.timeRightLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.mas_equalTo(self.timeLabel);
        
        make.left.mas_equalTo(self.timeLabel.mas_right).offset(10);
    }];
    
    /**
     开始年龄
     */
    [self.ageLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.mas_equalTo(self.whiteView);
        
        make.height.mas_equalTo(1);
        
        make.width.mas_equalTo(10);
        
        make.centerY.mas_equalTo(self.startAge);
    }];
    
    /**
     开始年龄
     */
    
    [self.startAge mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(self.todayBtn);
        
        make.bottom.mas_equalTo(self.timeLabel.mas_top).offset(-30);
        
        make.right.mas_equalTo(self.ageLineView.mas_left).offset(-3);
    }];
    
    
    /**
     结束年龄
     */
    [self.endAge mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.startAge);
        
        make.left.mas_equalTo(self.ageLineView.mas_right).offset(3);
        
        make.height.mas_equalTo(self.startBtn);
        
        make.right.mas_equalTo(self.monBtn);
    }];
    
    /**
     年龄
     */
    [self.ageLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.mas_equalTo(self.whiteView);
        
        make.bottom.mas_equalTo(self.startAge.mas_top).offset(-20);
        
    }];
    
    
    
    /**
     年龄
     */
    [self.ageLeftLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.mas_equalTo(self.ageLabel);
        
        make.right.mas_equalTo(self.ageLabel.mas_left).offset(-3);
    }];
    
    /**
     年龄
     */
    [self.ageRightLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.mas_equalTo(self.ageLabel);
        
        make.left.mas_equalTo(self.ageLabel.mas_right).offset(3);
    }];
    
    /**
     性别
     */
    [self.manBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.bottom.mas_equalTo(self.ageLabel.mas_top).offset(-35);
        
        make.right.mas_equalTo(self.whiteView.mas_centerX).offset(-20);
    }];
    
    /**
     性别
     */
    [self.womanBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.bottom.mas_equalTo(self.ageLabel.mas_top).offset(-35);
        
        make.left.mas_equalTo(self.whiteView.mas_centerX).offset(20);
    }];
    
    /**
     性别
     */
    [self.sexLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(self.whiteView);
        
        make.bottom.mas_equalTo(self.manBtn.mas_top).offset(-20);
        
    }];
    
    /**
     性别
     */
    [self.sexLeftLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.mas_equalTo(self.sexLabel);
        
        make.right.mas_equalTo(self.sexLabel.mas_left).offset(-3);
    }];
    
    /**
     性别
     */
    [self.sexRightLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self.sexLabel);
        
        make.left.mas_equalTo(self.sexLabel.mas_right).offset(3);
    }];
    
    
    
    /**
     标题
     */
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.mas_equalTo(self.whiteView);
        
        make.bottom.mas_equalTo(self.sexLabel.mas_top).offset(-30);
        
        make.top.mas_equalTo(self.whiteView).offset(12);
    }];
    
    
    /**
     关闭按钮
     */
    [self.closeBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.mas_equalTo(self.titleLabel);
        
        make.right.mas_equalTo(self.whiteView).offset(-15);
    }];
    
}


-(void)layoutSubviews{
    
    [self.manBtn titleBelowTheImageWithSpace:10];
    
    [self.womanBtn titleBelowTheImageWithSpace:10];
}


#pragma mark 快速日期选择
-(void)shortTimeBtnClick:(UIButton *)btn{
    
    NSInteger tag = btn.tag;
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";
    
    NSDate *date = [NSDate date];
    
    NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
    
    NSDateComponents *dateComponents = [calendar components:NSCalendarUnitDay fromDate:date];
    
    if (tag == 1) {
    
        self.todayBtn.backgroundColor = ButtonBackGroundColor;
        self.todayBtn.layer.borderColor = ButtonBackGroundColor.CGColor;
        self.weekBtn.backgroundColor = CSWhiteColor;
        self.weekBtn.layer.borderColor = CSColorFromRGB(0xdbdbdb).CGColor;
        self.monBtn.backgroundColor = CSWhiteColor;
        self.monBtn.layer.borderColor = CSColorFromRGB(0xdbdbdb).CGColor;
        [self.startBtn setTitle:[formatter stringFromDate:date]];
        [self.endBtn setTitle:[formatter stringFromDate:date]];
    }
    else if (tag == 2){
        
        self.todayBtn.backgroundColor = CSWhiteColor;
        self.weekBtn.backgroundColor = ButtonBackGroundColor;
        self.monBtn.backgroundColor = CSWhiteColor;
        
        self.weekBtn.layer.borderColor = ButtonBackGroundColor.CGColor;
        self.todayBtn.layer.borderColor = CSColorFromRGB(0xdbdbdb).CGColor;
        self.monBtn.layer.borderColor = CSColorFromRGB(0xdbdbdb).CGColor;
        [dateComponents setDay:-12];
        NSDate *newdate = [calendar dateByAddingComponents:dateComponents toDate:date options:0];
        [self.startBtn setTitle:[formatter stringFromDate:newdate]];
        [self.endBtn setTitle:[formatter stringFromDate:date]];
    }
    else{
        
        self.todayBtn.backgroundColor = CSWhiteColor;
        self.weekBtn.backgroundColor = CSWhiteColor;
        self.monBtn.backgroundColor = ButtonBackGroundColor;
        
        self.monBtn.layer.borderColor = ButtonBackGroundColor.CGColor;
        self.todayBtn.layer.borderColor = CSColorFromRGB(0xdbdbdb).CGColor;
        self.weekBtn.layer.borderColor = CSColorFromRGB(0xdbdbdb).CGColor;
        
        [dateComponents setDay:-30];
        NSDate *newdate = [calendar dateByAddingComponents:dateComponents toDate:date options:0];
        [self.startBtn setTitle:[formatter stringFromDate:newdate]];
        [self.endBtn setTitle:[formatter stringFromDate:date]];
    }
    
    [self.filtDict setObject:self.startBtn.title forKey:@"date1"];
    
    [self.filtDict setObject:self.endBtn.title forKey:@"date2"];
}

#pragma mark 日期选择
-(void)timeSelect:(UIButton *)btn{
    
    NSInteger tag = btn.tag;
    
    __weak typeof(self)weakSelf = self;
    
    [[MOFSPickerManager shareManger] showDatePickerWithTag:1120 title:@"选择日期" cancelTitle:@"取消" commitTitle:@"确定" datePickerMode:UIDatePickerModeDate commitBlock:^(NSDate *date) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];//创建一个日期格式化器
            
            dateFormatter.dateFormat=@"yyyy-MM-dd";//指定转date得日期格式化形式
            
            if (tag == 1) {
                
                if ([weakSelf compareOneDay:[dateFormatter stringFromDate:date] withAnotherDay:weakSelf.endBtn.title] > 0) {
                    
                    [MBProgressHUD showText:@"开始日期不能大于结束日期"];
                    
                    return ;
                }
                
                [weakSelf.startBtn setTitle:[dateFormatter stringFromDate:date]];
                
                [weakSelf.filtDict setObject:[dateFormatter stringFromDate:date] forKey:@"date1"];
            }
            else{
                
                if ([weakSelf compareOneDay:[dateFormatter stringFromDate:date] withAnotherDay:weakSelf.endBtn.title] < 0) {
                    
                    [MBProgressHUD showText:@"结束日期不能小于开始日期"];
                    
                    return ;
                }
                
                [weakSelf.endBtn setTitle:[dateFormatter stringFromDate:date]];
                
                [weakSelf.filtDict setObject:[dateFormatter stringFromDate:date] forKey:@"date2"];
            }
            
        });
        
    } cancelBlock:^{
        
        
    }];
}


#pragma mark 年龄选择
-(void)ageSelect:(UIButton *)ageBtn{
    
    NSInteger tag = ageBtn.tag;
    
    __weak typeof(self)weakSelf = self;
    
    [[MOFSPickerManager shareManger] showPickerViewWithDataArray:self.ageArray tag:1021 title:@"选择年龄" cancelTitle:@"取消" commitTitle:@"确定" commitBlock:^(NSString *string) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            if (tag == 1) {
                
                if ([string intValue] > [weakSelf.endAge.title intValue]) {
                    
                    [MBProgressHUD showText:@"开始年龄不能大于结束年龄"];
                    
                    return ;
                }
                
                [weakSelf.startAge setTitle:string];
                
//                [weakSelf.filtDict setObject:string forKey:@"startAge"];
                
            }
            else{
                
                if ([string intValue] < [weakSelf.startAge.title intValue]) {
                    
                    [MBProgressHUD showText:@"结束年龄不能小于开始年龄"];
                    
                    return ;
                }
                
                [weakSelf.endAge setTitle:string];
                
//                [weakSelf.filtDict setObject:string forKey:@"endAge"];
            }
            
            [weakSelf.filtDict setObject:[NSString stringWithFormat:@"%@-%@",weakSelf.startAge.title,weakSelf.endAge.title] forKey:@"endAge"];
            
        });
        
        
    } cancelBlock:^{
        
        
    }];
}

#pragma mark 性别按钮点击
-(void)sexBtnClick:(UIButton *)btn{
    
    NSInteger tag = btn.tag;
    
    if (tag == 1) {
        
        self.manBtn.selected = YES;
        
        self.womanBtn.selected = NO;
        
        [self.filtDict setObject:@"0" forKey:@"sex"];
    }
    else{
        
        self.manBtn.selected = NO;
        
        self.womanBtn.selected = YES;
        
        [self.filtDict setObject:@"1" forKey:@"sex"];
    }
}

-(int)compareOneDay:(NSString *)oneDay withAnotherDay:(NSString *)anotherDay

{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    
    NSDate *dateA = [dateFormatter dateFromString:oneDay];
    
    NSDate *dateB = [dateFormatter dateFromString:anotherDay];
    
    NSComparisonResult result = [dateA compare:dateB];
    
    //NSLog(@"date1 : %@, date2 : %@", oneDay, anotherDay);
    
    if (result == NSOrderedDescending) {
        
        //NSLog(@"Date1  is in the future");
        
        return 1;
        
    }
    
    else if (result ==NSOrderedAscending){
        
        //NSLog(@"Date1 is in the past");
        
        return -1;
        
    }
    
    //NSLog(@"Both dates are the same");
    
    return 0;
    
}

#pragma mark 关闭按钮点击
-(void)closeBtnClick{
    
    [self removeFromSuperview];
}

#pragma mark 重置
-(void)reset{
    
    [self.filtDict removeAllObjects];
    
    [self.filtDict setObject:@"0-30" forKey:@"age"];
    
    
    [self.filtDict removeObjectForKey:@"sex"];
    
    [self.filtDict removeObjectForKey:@"date1"];
    
    [self.filtDict removeObjectForKey:@"date2"];
    
    self.manBtn.selected = NO;
    
    self.womanBtn.selected = NO;
    
    [self.startAge setTitle:@"0"];
    
    [self.endAge setTitle:@"30"];
    
    [self.startBtn setTitle:@"开始日期"];
    
    [self.endBtn setTitle:@"结束日期"];
    
    self.todayBtn.backgroundColor = CSWhiteColor;
    self.weekBtn.backgroundColor = CSWhiteColor;
    self.monBtn.backgroundColor = CSWhiteColor;
    
    self.monBtn.layer.borderColor = CSColorFromRGB(0xdbdbdb).CGColor;
    self.todayBtn.layer.borderColor = CSColorFromRGB(0xdbdbdb).CGColor;
    self.weekBtn.layer.borderColor = CSColorFromRGB(0xdbdbdb).CGColor;
}

#pragma mark 确认按钮
-(void)sureBtnClick{
    
    if (self.filterBlock) {
        
        self.filterBlock(self.filtDict);
        
        [self removeFromSuperview];
    }
}

-(NSMutableArray *)ageArray{
    
    if (!_ageArray) {
        
        _ageArray = [[NSMutableArray alloc] init];
        
        for (int i = 1; i <= 100; i++) {
            
            [_ageArray addObject:[NSString stringWithFormat:@"%d",i]];
        }
    }
    
    return _ageArray;
}
@end
